package com.sosee.cgfw.friends;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import com.sosee.cgfw.R;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Thinkpad on 2016/3/6.
 */
public class FriendsSearchLocal extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private  ImageButton friends_back9;
    private EditText friends_search_http1;
    private TextView friends_search_ok1;
    private HttpUtils httpUtils;
    private String dd;
//    List<String> letterToCity=new ArrayList<String>();
    private ListView friends_list_searchs;

    ArrayList<NameData> name= new ArrayList<NameData>();
    ArrayList<NameData> name1= new ArrayList<NameData>();
    Handler myhandler = new Handler();
    private  MysAdapter mysAdapter;

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends_searchlocal);
        httpUtils =new HttpUtils();
        init();
        initData();
    }

    private void init() {


            dd = SharedPreferencesUtils.getString(FriendsSearchLocal.this, "dd");
        if(dd!=null){
            Gson gson = new Gson();
            FriendsListData friendsListData = gson.fromJson(dd, FriendsListData.class);
            List<FriendsListData.Data> data = friendsListData.getData();

            NameData namedata;
            for(int i=0;i<data.size();i++){
                namedata= new NameData();
                namedata.setName(data.get(i).getFriends().getNickname());
                namedata.setId(data.get(i).getFriends().getID());
                name.add(namedata);

            }


                for(int i=0;i<data.size();i++){
                    System.out.println( "被存了一次的好友" + name.get(i).getName());
                }
        }

    }

    private void initData() {
        friends_back9=(ImageButton)findViewById(R.id.friends_back9);
        friends_search_http1=(EditText)findViewById(R.id.friends_search_http1);
        friends_search_ok1= (TextView)findViewById(R.id.friends_search_ok1);
        friends_list_searchs=(ListView)findViewById(R.id.friends_list_searchs);

        mysAdapter = new MysAdapter();
        friends_list_searchs.setAdapter(mysAdapter);
        friends_list_searchs.setOnItemClickListener(this);

        friends_back9.setOnClickListener(this);

        friends_search_ok1.setOnClickListener(this);
        friends_search_http1.addTextChangedListener(watcher);



    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.friends_search_ok1:
                //这里是遍历本地的好友。

                finish();


                break;
            case R.id.friends_back9:
                finish();

                break;
            default:

                break;
        }
    }

    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //当监听到text发生变化，就更新界面。
        }



        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {

            //用获取到的数据和list里面的getname进行比较。
            System.out.println("afterChanged-->" + friends_search_http1.getText().toString() + "<--");
            myhandler.post(eChanged);
        }
    };

    Runnable eChanged = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            String data1 = friends_search_http1.getText().toString();

            name1.clear();//先要清空，不然会叠加

            getmDataSub(data1);//获取更新数据
            System.out.println("走了这里");

            mysAdapter.notifyDataSetChanged();//更新

        }
    };

    //监听过滤。
    private void getmDataSub(String data)
    {
        NameData namedata1;
        for(int i = 0; i <name.size(); ++i){
            namedata1= new NameData();
            //吧监听到的字符传进来进行遍历，包含这个字符的对象就存到另外个数组里免去。
            if(name.get(i).getName().contains(data)){
                namedata1.setName(name.get(i).getName());
                namedata1.setId(name.get(i).getId());
                name1.add(namedata1);
            }

        }
        for(int i=0;i<name1.size();i++){
            System.out.println("这是第二次被过滤后好友："+name1.get(i).getName());
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.friends_list_searchs:

                //打开个人信息窗口，然后点击下面的发送按钮的话会跳转到发送消息页面
                Intent intent = new Intent(FriendsSearchLocal.this,FriendsInfo.class);
                //吧点击条目的id获取到传递进下一个页面。
                intent.putExtra("Id",name1.get(position).getId());

                startActivity(intent);

                break;
        }
    }

    public class MysAdapter extends BaseAdapter{
        @Override
        public int getCount() {

            return name1==null?0:name1.size();
        }

        @Override
        public Object getItem(int position) {
            return name1.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


            ViewHolder holder;
            if (convertView == null) {

                holder = new ViewHolder();
                convertView = LayoutInflater.from(FriendsSearchLocal.this).inflate(R.layout.friends_myfriends_item, null);

                holder.friends_friendsname = (TextView) convertView.findViewById(R.id.friends_friendsname);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.friends_friendsname.setText(name1.get(position).getName());

            return convertView;

        }


        class ViewHolder {
            public TextView friends_friendsname;

        }
    }
}


